package com.qianchang.microfinance.view.mine.loan;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dm.library.adapter.CommonAdapter;
import com.dm.library.adapter.CommonViewHolder;
import com.dm.library.utils.FormatUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.RepaymentPlanAfterBean;
import com.qianchang.microfinance.bean.RepaymentPlanBean;

import java.util.List;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:
 * 修 改 人: zhoulai
 * 修改时间: 2017/7/6 9:12
 */
public  class RepaymentPlanAdapter extends CommonAdapter {

    public RepaymentPlanAdapter(Context mContext, List beanList, int layoutRes) {
        super(mContext, beanList, layoutRes);
    }

    @Override
    protected void convert(CommonViewHolder holder, Object o, int position) {
        if (o instanceof RepaymentPlanBean.Item) {//贷前
            RepaymentPlanBean.Item item = (RepaymentPlanBean.Item) o;
            String str = mContext.getString(R.string.Label_principal) + " " + FormatUtil.numKbPointFormat(item.getBenjin()) + "  "
                    + mContext.getString(R.string.Label_interest) + " " + FormatUtil.numKbPointFormat(item.getLixi());
            holder.setText(R.id.tv_principal_interest, str);
            holder.setText(R.id.tv_date, handleDate(item.getDateTime()));////2017-09-28 00:00:00
            String str2 = Double.valueOf(item.getBenjin()) + Double.valueOf(item.getLixi()) + "";
            holder.setText(R.id.tv_plan_amount, FormatUtil.numKbPointFormat(str2));
        } else if (o instanceof RepaymentPlanAfterBean) {//贷后

            //还款记录时间：realPayDate	String	还款时间（格式化之后）
            //还款计划时间：shouldPayDate	String	应还时间(格式化之后)
            RepaymentPlanAfterBean bean = (RepaymentPlanAfterBean) o;
            if (bean == null) return;
            String str = mContext.getString(R.string.Label_principal) + " " + FormatUtil.numKbPointFormat(bean.getPrincipal()) + "  "
                    + mContext.getString(R.string.Label_interest) + " " + FormatUtil.numKbPointFormat(bean.getInterest());
            holder.setText(R.id.tv_principal_interest, str);
            holder.setText(R.id.tv_date, handleDate(bean.getShouldPayDate()));
            holder.setText(R.id.tv_plan_amount, FormatUtil.numKbPointFormat(bean.getPrincipal() + bean.getInterest()));
            setStatus(bean,holder);
        }

    }

    private void setStatus(RepaymentPlanAfterBean bean, CommonViewHolder commonViewHolder) {
        String replyMode = "";
        //  private String mode;//还款类型有，包括：0 是未还款、1是已还款、status=0并且overdu是1就是逾期；
        if(bean.getStatus().equals("0") && !(bean.getIsOverdue().equals("1"))){
            replyMode = "未还款";
            commonViewHolder.setTextColor(R.id.tv_status_id,R.color.common_black_color_9);
            commonViewHolder.setText(R.id.tv_status_id,replyMode);
        }else if(bean.getStatus().equals("1") && (bean.getIsOverdue().equals("0"))){
            replyMode = "已还款";
            commonViewHolder.setTextColor(R.id.tv_status_id,R.color.common_black_color_9);
            commonViewHolder.setText(R.id.tv_status_id,replyMode);
        }else if(bean.getStatus().equals("0") && bean.getIsOverdue().equals("1")){
            replyMode = "逾期中";
            commonViewHolder.setTextColor(R.id.tv_status_id,R.color.common_red_color);
            commonViewHolder.setText(R.id.tv_status_id,replyMode);
        }else if(bean.getStatus().equals("1") && bean.getIsOverdue().equals("1")){
            replyMode = "已还款";
            commonViewHolder.setTextColor(R.id.tv_status_id,R.color.common_black_color_9);
            commonViewHolder.setText(R.id.tv_status_id,replyMode);
        }

    }



    private String handleDate(String shouldPayDate) {
        String strDate = "";
        if (shouldPayDate != null && shouldPayDate.length() >= 10) {
            String strD = shouldPayDate.substring(0, 10);
            //strDate = strD.replaceFirst("-", "\n");
            strDate = strD;
        } else {
            strDate = shouldPayDate;
        }
        return strDate;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
        if (beanList != null && beanList.size() != 0) {
            if (position > beanList.size() - 1) {
                convert((CommonViewHolder) holder, null, position);
            } else {
                convert((CommonViewHolder) holder, beanList.get(position), position);
            }
        }
    }
}
